package com.ctsi.commons.conversion;

import com.ctsi.commons.util.ObjectType;

 

/** Abstract Converter class. This class handles converter registration
 * and it implements the <code>canConvert</code>, <code>getSourceClass</code>,
 * and <code>getTargetClass</code> methods.
 */
public abstract class AbstractConverter<S, T> implements Converter<S, T>, ConverterLoader {
    private final Class<? super S> sourceClass;
    private final Class<? super T> targetClass;

    protected AbstractConverter(Class<? super S> sourceClass, Class<? super T> targetClass) {
        this.sourceClass = sourceClass;
        this.targetClass = targetClass;
    }
    @Override
    public void loadConverters() {
        Converters.registerConverter(this);
    }
    @Override
    public T convert(final Class<? extends T> targetClass,final  S obj) throws ConversionException {
        return convert(obj);
    }
    @Override
    public boolean canConvert(final Class<?> sourceClass,final  Class<?> targetClass) {
        return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && ObjectType.instanceOf(targetClass, this.getTargetClass());
    }
    @Override
    public Class<? super S> getSourceClass() {
        return sourceClass;
    }
    @Override
    public Class<? super T> getTargetClass() {
        return targetClass;
    }
}
